#include "tcp_service.h"
#include <stdexcept>
#include <string.h>

RTSPServer::RTSPServer() {
    // 初始化成员变量
}

bool RTSPServer::init(const std::string &ip_address) {
    struct sockaddr_in rtsp_address = {};
    
    // 创建套接字
    this->rtsp_sock = socket(AF_INET, SOCK_STREAM, 0);
    if (this->rtsp_sock < 0) {
        std::cerr << "无法创建RTSP套接字" << std::endl;
        return false;
    }

    // 设置地址和端口
    if (inet_pton(AF_INET, ip_address.c_str(), &rtsp_address.sin_addr) <= 0) {
        std::cerr << "无效的IP地址: " << ip_address << std::endl;
        return false;
    }
    rtsp_address.sin_family      = AF_INET;
    rtsp_address.sin_port        = htons(RTSP_PORT);
    rtsp_address.sin_addr.s_addr = INADDR_ANY;

    // 设置套接字选项
    int option = 1;
    socklen_t optlen = sizeof(option);
    if (setsockopt(this->rtsp_sock, SOL_SOCKET, SO_REUSEADDR, (void *)&option, optlen) < 0) {
        perror("setsockopt SO_REUSEADDR");
        return false;
    }
    if (setsockopt(this->rtsp_sock, SOL_SOCKET, SO_REUSEPORT, (void *)&option, optlen) < 0) {
        perror("setsockopt SO_REUSEPORT");
        return false;
    }

    // 绑定套接字
    if (bind(this->rtsp_sock, (struct sockaddr *)&rtsp_address, sizeof(rtsp_address)) < 0) {
        perror("bind");
        return false;
    }

    // 监听套接字
    if (listen(this->rtsp_sock, 3) < 0) {
        perror("listen");
        return false;
    }

    std::cout << "RTSP服务器已启动，正在监听 " << ip_address << ":" << RTSP_PORT << std::endl;
    return true;
}

RTSPServer::~RTSPServer() {
    if (this->rtsp_sock != -1) {
        close(this->rtsp_sock);
    }
}

void RTSPServer::start() {
    int addrlen = sizeof(rtsp_address);
    int new_socket = accept(this->rtsp_sock, (struct sockaddr *)&rtsp_address, (socklen_t *)&addrlen);
    if (new_socket < 0) {
        perror("accept");
        std::cerr << "接受RTSP连接失败" << std::endl;
        throw std::runtime_error("接受RTSP连接失败");
    }
    handle_client(new_socket);
}

void RTSPServer::handle_client(int new_socket) {
    char buffer[1024] = {0};
    const char *describe_response = "RTSP/1.0 200 OK\r\nCSeq: 1\r\nContent-Type: application/sdp\r\nContent-Length: 0\r\n\r\n";

    read(new_socket, buffer, 1024);
    std::cout << "接收到DESCRIBE请求：" << std::endl;
    std::cout << buffer << std::endl;
    send(new_socket, describe_response, strlen(describe_response), 0);
    std::cout << "发送DESCRIBE响应" << std::endl;
    close(new_socket);

    // 创建RTP套接字并发送数据
    int rtp_sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (rtp_sock < 0) {
        std::cerr << "无法创建RTP套接字" << std::endl;
        return;
    }
    struct sockaddr_in rtp_address;
    rtp_address.sin_family = AF_INET;
    rtp_address.sin_addr.s_addr = inet_addr("239.0.0.1");
    rtp_address.sin_port = htons(RTP_PORT);

    while (true) {
        char rtp_data[1500] = "模拟RTP数据包";
        sendto(rtp_sock, rtp_data, strlen(rtp_data), 0, (struct sockaddr *)&rtp_address, sizeof(rtp_address));
        std::cout << "发送RTP数据包" << std::endl;
        sleep(1);  // 模拟视频流
    }
    close(rtp_sock);
}
